package com.atguigu.test.boot.bean;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.StringUtils;

/**
 * @author zp
 * @version 1.0
 * @description: 校验mybatis所需要的参数
 * @date 2023/2/9 19:06
 */
public class MybatisParameterVerifyConditional implements Condition{
    public static final String URL="jdbc.url";
    public static final String USER_NAME="jdbc.username";
    public static final String PASSWORD="jdbc.password";
    /**
     * @description: 校验mybatis 所需要的参数 否则注入失败
     * @author zp
     * @date: 2023/2/10 19:28
     * @param context
     * @param metadata
     * @return boolean
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        String url = environment.getProperty(URL);
        String userName = environment.getProperty(USER_NAME);
        String passWord = environment.getProperty(PASSWORD);
        return !(StringUtils.isEmpty(url) ||
                StringUtils.isEmpty(userName) ||
                StringUtils.isEmpty(passWord));
    }


}
